/********************************************************************************
* File Name:	paymentfrm.cpp
* Description:	第8章实例训练          
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1  
* E_mail: openlinux2011@gmail.com		                
*
********************************************************************************/
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QFileDialog>
#include <QTextCodec>
#include <QTextStream>
#include <QDate>
#include "paymentfrm.h"
#include "ui_paymentfrm.h"

//会员管理界面构造函数
paymentFrm::paymentFrm(QWidget *parent) :QDialog(parent),ui(new Ui::paymentFrm)
{
    ui->setupUi(this);
    QSqlQuery query ;
    query.exec("select id from Customer");
    while(query.next()) //将数据库中会员的ID号与QComboBox绑定
    {
        ui->VIPID_2->insertItem(0,query.value(0).toString());
    }
    query.exec("select ISBN from Book");
    while(query.next()) //将所有图书的ISBN与QComboBox绑定
    {
        ui->ISBN_2->insertItem(0,query.value(0).toString());
    }
    clickNum=0; //初始化点击次数
}

paymentFrm::~paymentFrm()
{
    delete ui;
}

void paymentFrm::setDiscount()//将折扣数设置成与会员等级相对应的数字
{
    if(clickNum==0) //会员
    {
        QSqlQuery query;
        query.exec("select Levels from Customer where id="+ui->VIPID_2->currentText());//根据会员的ID获取其级别
        query.next();
        QVariant level=query.value(0);
        query.exec("select Discount from Discount where Levels="+level.toString()); //根据级别查询其打折信息
        query.next();
        QString discount;
        discount=discount.setNum(query.value(0).toDouble(),'f',2);
        ui->Discount_2->setText(discount);
        clickNum=1; //记录点击次数
    }
    else //非会员不打折
    {
         ui->Discount_2->setText("1.00");
         clickNum=0; //将点击次数清0
    }
}

//客户购买图书时，将销售图书信息及相关信息显示的界面的一个表中
void paymentFrm::setTable()//更新table中的条目
{

    QSqlQuery query;
    query.exec("select * from Book where ISBN="+ui->ISBN_2->currentText()); //根据图书的ISBN查询图书信息
    query.next();
    int i=ui->Table->rowCount(); //获取Table的当前行顺
    ui->Table->insertRow(i); //插入新的一行

    /*设置表格显示售书信息*/
    ui->Table->setItem(i,0,new QTableWidgetItem(query.value(1).toString()));
    ui->Table->setItem(i,1,new QTableWidgetItem(query.value(2).toString()));
    ui->Table->setItem(i,2,new QTableWidgetItem(query.value(6).toString()));
    ui->Table->setItem(i,3,new QTableWidgetItem(ui->Number_2->text()));
    ui->Table->setItem(i,4,new QTableWidgetItem(ui->Discount_2->text()));
    ui->Table->setItem(i,6,new QTableWidgetItem(query.value(0).toString()));
    int num=ui->Number_2->text().toInt(); //图书数量
    float discount=ui->Discount_2->text().toFloat(); //折扣信息
    float total=num*discount*query.value(6).toInt(); //计算消费
    QString Total;
    Total=Total.setNum(total,'f',2);
    ui->Table->setItem(i,5,new QTableWidgetItem(Total)); //将消费金额加入表格
}


void paymentFrm::removeTable()//清空table中的内容
{
    ui->Table->setRowCount(0);
}

void paymentFrm::finish()//点击"完成"后执行输出小票，并更改Sell表
{
    QString fn =QFileDialog::getSaveFileName(this,QString::null, QString::null); //获取保存信息的文件名
    if(fn.isEmpty()) //没有设定文件名
       return;
    QFile f(fn); //生成文件
    if(!f.open( QIODevice::WriteOnly)) //以写的权限打开文件
        return;
    QTextStream t(&f); //文件流
    t.setCodec(QTextCodec::codecForLocale()); //设置文件编码格式
    t.setFieldWidth(20); //设置文件流的字段宽度
    QDate date=QDate::currentDate(); //获取当前日期
    t<<date.toString(tr("yyyy 年 MM 月 dd 日"))<<"\n";
    t<<"BookID     Name       Price    amount   Discount      total"<<"\n";
    t<<"-----------------------------------------------------------"<<"\n";

    float sum=0;
    QSqlQuery query;
    QString CustomerID,BookID,Num,Discount,Sum,insertData;

    if(ui->VIPID->isChecked()) //判断是否是会员购买图书
    {
        for(int i=0;i<ui->Table->rowCount();i++) //将购买的图书信息插入到Sell表中
        {
            CustomerID=ui->VIPID_2->currentText(); //会员号

            /*获取表中的信息*/
            BookID=ui->Table->item(i,6)->text().toLatin1();
            Num=ui->Table->item(i,3)->text().toLatin1();
            Discount=ui->Table->item(i,4)->text().toLatin1();
            Sum=ui->Table->item(i,5)->text().toLatin1();
            sum+=ui->Table->item(i,5)->text().toFloat();
            insertData="insert into Sell(CustomerID,BookID,Num,Discount,Sum,Time) values("+CustomerID+","+BookID+","+Num+","+Discount+","+Sum+",'"+tr("%1").arg(date.year())+"-"+tr("%1").arg(date.month())+"-"+tr("%1").arg(date.day())+"')";        
            if(query.exec(insertData)) //执行插入
            {
                t<<ui->Table->item(i,0)->text().toLatin1()+"      "+ui->Table->item(i,1)->text()+"        "+ui->Table->item(i,2)->text().toLatin1()+"        " +Num+"        "
                       +Discount+"        " +Sum+"        \n"; //将信息写入文件流

                query.exec("select  Storage from Book  where id="+BookID); //更改图书的库存
                int sell_num=ui->Table->item(i,3)->text().toInt();
                int num ;
                if(query.next())
                {
                    num=query.value(0).toInt();
                    num=num-sell_num;
                    query.exec("update  Book  set Storage = " + tr("%1").arg(num) +" where id= " + BookID);
                }
            }
            else
                return;
        }
    }
    else //非会员购买图书
    {
        for(int i=0;i<ui->Table->rowCount();i++) //将购买的图书信息插入到Sell表中
        {
            /*获取表中的信息*/
            BookID=ui->Table->item(i,6)->text().toLatin1();
            Num=ui->Table->item(i,3)->text().toLatin1();
            Discount=ui->Table->item(i,4)->text().toLatin1();
            Sum=ui->Table->item(i,5)->text().toLatin1();
            sum+=ui->Table->item(i,5)->text().toFloat();
            insertData="insert into Sell(BookID,Num,Discount,Sum,Time) values("+BookID+","+Num+","+Discount+","+Sum+",'"+tr("%1").arg(date.year())+"-"+tr("%1").arg(date.month())+"-"+tr("%1").arg(date.day())+"')";
            if(query.exec(insertData)) //执行插入
            {
                t<<ui->Table->item(i,0)->text().toLatin1()+"      "+tr("%1").arg(ui->Table->item(i,1)->text())+"        "+ui->Table->item(i,2)->text().toLatin1()+"        " +Num+"        "
                       +Discount+"        " +Sum+"        \n"; //将信息写入文件流

                query.exec("select  Storage from Book  where id="+BookID); //更改图书的库存
                int sell_num=ui->Table->item(i,3)->text().toInt();
                int num ;
                if(query.next())
                {
                    num=query.value(0).toInt();
                    num=num-sell_num;
                    query.exec("update  Book  set Storage = " + tr("%1").arg(num) +" where id= " + BookID);
                }
            }
            else
                return;
        }
    }
    t<<"-----------------------------------------------------------"<<"\n";
    t<<tr("合计：                                                  ")+tr("%1").arg(sum)<<"\n";
    t<<"You are always welcome!"<<"\n";
    f.close();//关闭文件
}
